package com.zt.ftphelper.util;

import com.zt.ftphelper.Config;
import com.zt.ftphelper.model.ServerItem;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zt
 * @Description:
 * @date 31/08/2019 11:45
 */
public class Tools {

    public static ServerItem getServerItemFromString(Integer id,String serverAdnpath){
        String server = serverAdnpath.substring(0,serverAdnpath.indexOf("|"));
        String depolyPath = serverAdnpath.substring(serverAdnpath.indexOf("|")+1);
        String userName = server.substring(6,server.indexOf(":",6));
        String password = server.substring(server.indexOf(":",6)+1,server.indexOf("@"));
        String ip = server.substring(server.indexOf("@") + 1, server.lastIndexOf(":"));
        String port = server.substring(server.lastIndexOf(":")+1);
        return new ServerItem(id,ip,port,userName,password,depolyPath);
    }

    public static void openFolderInExplorer(String location){
        try {
            Runtime.getRuntime().exec("explorer /select, " + location);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static char byteAsciiToChar(int ascii){
        char ch = (char)ascii;
        return ch;
    }

    public static String getDeployLocation(String deployPath,String filePath){
        String basePath = Config.BASE_PATH;
        String relativePath = filePath.replace(basePath,"");
         if(deployPath.endsWith("/")){
            deployPath=deployPath.substring(0, deployPath.length() - 1);
        }
        String result = deployPath+relativePath.replace("\\","/");
        return result;
    }

    public  static File generateFile() {
        String programPath  = new File(Tools.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParent();
        File outPutPath = new File(programPath+File.separator+"output");
        if(!outPutPath.exists()){
            outPutPath.mkdir();
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy_MM_dd_HH_MM_SS");
        SimpleDateFormat dfTime = new SimpleDateFormat("yyyy-MM-dd:HH:MM:SS  ");
        String fileName=df.format(new Date())+".fqf";
        File output = new File(outPutPath.getAbsolutePath(),fileName);

        if(!output.exists()){
            try {
                output.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            FileUtils.writeStringToFile(output,getOutputString(),"GBK");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return output;
    }

    public static String getOutputString(){
        StringBuilder sb = new StringBuilder();
        Config.getInstance().getTaskData().forEach(task->{
            File f = new File(task.getPath());
            Config.getInstance().getServerData().forEach(serverItem->{
                String str =  serverItem.toString();
                String server =str.substring(0, str.indexOf("|"));

                String deployPath = str.substring(str.indexOf("|")+1);
                //type
                sb.append(f.isFile()?"0":"1")
                        .append((char)1 )
                        //direction
                        .append("4")
                        .append((char)1).append((char)2)
                        //source host
                        .append("-")
                        .append((char)1).append((char)2)
                        .append(server);

                //target host string
                sb.append((char)1)
                        .append(f.getPath())
                        .append((char)1)
                        .append(Tools.getDeployLocation(deployPath,f.getPath()))
                        .append((char)1)
                        .append("4")
                        .append((char)1)
                        .append("0")
                        .append((char)1)
                        .append((char)1)
                        .append("0")
                        .append("\n");
            });
        });
        return sb.toString();
    }


    public static void main(String[] args) {

    }

}
